package com.itheima.mall.dubbo.api.sms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mall.commons.utils.StringUtils;
import com.itheima.mall.dubbo.mapper.sms.HomeRecommendProductMapper;
import com.itheima.mall.entity.domain.sms.HomeRecommendProduct;
import com.itheima.mall.entity.domain.vo.smsre.ResultAll;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;


@DubboService
public class HomeRecommendProductServiceImpl extends ServiceImpl<HomeRecommendProductMapper, HomeRecommendProduct> implements HomeRecommendProductService {
    @Autowired
    private HomeRecommendProductMapper homeRecommendProductMapper;

    @Override
    public void updateSort(Long id, Integer sort) {
        HomeRecommendProduct homeRecommendProduct = new HomeRecommendProduct();
        homeRecommendProduct.setId(id);
        homeRecommendProduct.setSort(sort);
        homeRecommendProductMapper.updateById(homeRecommendProduct);
    }

    @Override
    public ResultAll page(Integer pageNum, Integer pageSize, Integer recommendStatus, String productName) {
        Page<HomeRecommendProduct> homeRecommendProductPage = new Page<>(pageNum, pageSize);
        ResultAll resultAll = new ResultAll();
        LambdaQueryWrapper<HomeRecommendProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(productName), HomeRecommendProduct::getProductName, productName);
        queryWrapper.eq(recommendStatus != null, HomeRecommendProduct::getRecommendStatus, recommendStatus);

        homeRecommendProductMapper.selectPage(homeRecommendProductPage, queryWrapper);
        resultAll.setList(homeRecommendProductPage.getRecords());
        resultAll.setPageNum(pageNum);
        resultAll.setPageSize(pageSize);
        resultAll.setTotal(homeRecommendProductPage.getTotal());
        resultAll.setTotalPage(homeRecommendProductPage.getPages());
        return resultAll;
    }
}
